<?php
namespace Cli\Controller;
class TaskController extends CommonController {
    public function run() {

        // 获取没有执行的任务列表
        $Task = M('Task');
        $taskList = $Task->join('parse on parse.id = task.parseid')
                         ->field("task.*,parse.title as parse_title,parse.source_name,parse.source_url,parse.config")
                         //->where("task.status = 1 and task.updatetime < " . NOW_TIME)
                         ->where("task.status = 1")
                         ->limit(100)
                         ->select();

        if(!$taskList) {
            \Org\Util\Loger::error(array(
                'message'  =>  '没有需要执行的任务',
            ));
            return ;
        }

        foreach($taskList as $taskInfo) {
            $parseConfig = isset($taskInfo['config']) ? json_decode($taskInfo['config'],true) : array();

            $Task->save(array(
                'id'    =>  $taskInfo['id'],
                'updatetime'    =>  NOW_TIME + 86400 * $taskInfo['timespan']
            ));

            if(!$parseConfig) {
                \Org\Util\Loger::error(array(
                    'message'   =>  '解析配置不存在',
                    'data'      =>  $taskInfo,
                ));
                continue;
            }
            $urls = explode("\n",$taskInfo['urls']);
            if(!$urls) {
                \Org\Util\Loger::error(array(
                    'message'   =>  '解析配置中的url为空',
                    'data'      =>  $taskInfo,
                ));
                continue;
            }
            $this->fetchArticle($urls,$parseConfig,$taskInfo);
        }
    }

    /**
     * 根据任务配置抓取文章数据
     *
     * @param    void
     * @return    void
     **/
    protected function fetchArticle($urls,$parseConfig,$taskInfo) {
        static $ArticleParse = null;
        
        if($ArticleParse === null) {
            vendor('Util.ArticleParse');
            $ArticleParse = new \ArticleParse();
        }
        // 设置抓取参数
        $ArticleParse->setConfig($parseConfig);
        // 遍历文章列表抓取文章数据
        foreach($urls as $article_list_url) {
            // 获取文章列表数据
            $articleList = $ArticleParse->getList($article_list_url);
            if(!$articleList) {
                \Org\Util\Loger::error(array(
                    'message'   =>  'url中的文章列表解析失败',
                    'data'      =>  $article_list_url,
                ));
                continue;
            }
            foreach($articleList as $article) {
                $articleExists = $this->articleExists($article['title'],$taskInfo['cid']);
                if($articleExists) {
                    \Org\Util\Loger::error(array(
                        'message'   =>  '文章已经存在',
                        'url'       =>  $article['source_url'],
                        'title'     =>  $article['title'],
                        'cid'       =>  $taskInfo['cid'],
                    ));
                    continue;
                }
                try{
                    $articleInfo = $ArticleParse->getPage($article['source_url']);
                }catch(\Exception $e) {
                    \Org\Util\Loger::error(array(
                        'message'   =>  $e->getMessage() . '[file:' . $e->getFile() . ' | line:' . $e->getLine() . ']',
                        'url'       =>  $article['source_url'],
                        'task_id'   =>  $taskInfo['id'],
                        'task_title'=>  $taskInfo['title']
                    ));
                    continue;
                }
                $articleInfo && $article = array_merge($article,$articleInfo);
                $article['source_name'] = $ArticleParse->getSourceName();
                $article['cid'] = $taskInfo['cid'];
                $result = $this->addArticle($article);

            }
        }
    }
    /**
     * 文章是否已经存在
     *
     * @param    void
     * @return    void
     **/
    public function articleExists($title,$cid) {
        $token = substr(md5(NOW_TIME. C('API_SECRET_KEY')),0,8);

        $api = C('API_DOMAIN') . 'article/exists/' . C('API_SECRET_CODE') . '/' . $token . '/' . NOW_TIME;

        $data = post($api,array(
            'title' =>  $title,
            'cid'   =>  $cid
        ));
        if($data['code'] == 200) {
            return true;
        }else {
            return false;
        }
    }
    /**
     * 添加文章到文章库中
     *
     * @param    void
     * @return    void
     **/
    public function addArticle($post) {
        $token = substr(md5(NOW_TIME. C('API_SECRET_KEY')),0,8);

        $api = C('API_DOMAIN') . 'article/add/'. C('API_SECRET_CODE') . '/' . $token . '/' . NOW_TIME;

        $data = post($api,$post);

        if($data['code'] == 200) {
            \Org\Util\Loger::success(array(
                'message'   =>  '文章添加成功',
                'title'     =>  $post['title'],
                'cid'       =>  $post['cid'],
            ));
        }else {
            \Org\Util\Loger::error(array(
                'message'   =>  $data['message'],
                'title'     =>  $post['title'],
                'cid'       =>  $post['cid'],
            ));
        }
    }
}
